package com.ruoyi.web.init;

import com.ruoyi.biz.domain.BizCost;
import com.ruoyi.biz.domain.BizProject;
import com.ruoyi.biz.domain.BizTrustee;
import com.ruoyi.biz.enums.CostTypeEnum;
import com.ruoyi.biz.mapper.BizCostMapper;
import com.ruoyi.biz.mapper.BizTrusteeMapper;
import com.ruoyi.biz.service.BizCostService;
import com.ruoyi.biz.service.BizProjectService;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.utils.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Component;

import java.io.File;
import java.io.InputStream;
import java.math.BigDecimal;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
import java.util.List;

import static com.ruoyi.common.utils.SecurityUtils.getUserId;

/**
 * @author tianl
 * @date 2025/1/10
 * @description:
 */
@Slf4j
@Component
public class Init implements ApplicationRunner {
    @Autowired
    private BizProjectService bizProjectService;
    @Autowired
    private BizCostMapper bizCostMapper;
    @Autowired
    private BizTrusteeMapper bizTrusteeMapper;
    @Autowired
    private BizCostService bizCostService;

    @Override
    public void run(ApplicationArguments args) throws Exception {
        File desc = new File(RuoYiConfig.getTemplatePath());
        if (!desc.exists()) {
            desc.mkdirs();
        }
        save(desc.getAbsolutePath(), "excel/download.xls");
        save(desc.getAbsolutePath(), "excel/template.xls");
//        List<BizProject> projects = bizProjectService.list();
//        for (BizProject bizProject : projects) {
//            if (bizProject.getRealAmount() == null) {
//                bizProject.setRealAmount(bizProject.getTotalAmount());
//            }
//            bizProject.setRealIncome(bizProject.getRealAmount());
//            bizCostService.delByProjectId(bizProject.getId());
//            BigDecimal channelMoney = BigDecimal.ZERO;
//            if (bizProject.getTotalAmount() != null && bizProject.getRealAmount() != null) {
//                channelMoney = bizProject.getTotalAmount().subtract(bizProject.getRealAmount());
//            }
//            BigDecimal taxMoney = BigDecimal.ZERO;
//            if (bizProject.getTrusteeId() != null) {
//                BizTrustee trustee = bizTrusteeMapper.selectById(bizProject.getTrusteeId());
//                if (trustee != null) {
//                    taxMoney = channelMoney.multiply(trustee.getTaxProportion());
//                }
//            }
//            bizCostService.calcProjectDefault(bizProject.getId(), bizProject.getUserId());
//            bizProject.setChannelCost(channelMoney);
//            bizProject.setChannelTax(taxMoney);
//            bizProject.setRealIncome(bizProject.getRealAmount().subtract(taxMoney));
//            bizProjectService.saveOrUpdate(bizProject);
//            bizProjectService.calcProjectCost(bizProject.getId());
//        }
    }

    private void save(String targetDirectory, String fileName) {
        try {
            Resource resource = new ClassPathResource(fileName);
            InputStream inputStream = resource.getInputStream();
            File directory = new File(targetDirectory);
            if (!directory.exists()) {
                directory.mkdirs();
            }
            File targetFile = new File(directory, resource.getFilename());
            Files.copy(inputStream, targetFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
            log.info(fileName + "模板文件初始化成功！");
        } catch (Exception e) {
            log.error(fileName + "模板文件初始化失败！", e);
        }
    }
}
